package com.asb.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.asb.model.PeopleInfo;
import com.asb.model.vo.UserVo;
import com.asb.utils.JsonUtil;
import com.asb.utils.LocationEnum;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

/**
 * @author lenovo
 * @create 2022/3/23 11:27
 */
public class PeopleService {
	@Inject
	UserDingTalkService userDingTalkService;

	public List<UserVo> getPeopleInfoByPhone(String phone) {

		List<Record> rList = Db.find("SELECT \n" + "a.USERNAME AS username\n" + ",a.NAME AS name\n"
				+ ",a.MOBILE AS mobile\n" + ",a.EMAIL AS email\n" + ",a.LOCATIONID AS location\n"
				+ ",a.DEPARTMENT_ID AS department\n" + ",d.NAME AS deparmentname\n" + ",c.ID AS stationid\n"
				+ ",c.NAME AS station\n" + "from app_user a\n" + "inner join app_group_users  b\n"
				+ "on a.id=b.USER_ID\n" + "inner join app_group c\n" + "on b.GROUP_ID=c.id\n"
				+ "inner JOIN app_department d\n" + "ON a.DEPARTMENT_ID=d.ID\n" + "WHERE a.MOBILE =?", phone);
		List<UserVo> users = rList.stream().map(m -> {
			m.set("locationName", LocationEnum.getName(m.getStr("location")));
			return BeanUtil.fillBeanWithMapIgnoreCase(m.toMap(), new UserVo(), true);
		}).toList();

		return users;

	}

	public List<Map<String, Object>> getPeopleInfoByPhone(PeopleInfo peopleInfo) {
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			for (String phone : peopleInfo.getMobile()) {
				List<Map<String, Object>> listObject = new ArrayList<>();
				List<Record> users1 = Db.find("SELECT \n" + "a.USERNAME AS username\n" + ",a.NAME AS name\n"
						+ ",a.MOBILE AS mobile\n" + ",a.EMAIL AS email\n" + ",a.LOCATIONID AS location\n"
						+ ",a.DEPARTMENT_ID AS department\n" + ",d.NAME AS deparmentname\n" + ",c.ID AS stationid\n"
						+ ",c.NAME AS station\n" + "from app_user a\n" + "inner join app_group_users  b\n"
						+ "on a.id=b.USER_ID\n" + "inner join app_group c\n" + "on b.GROUP_ID=c.id\n"
						+ "inner JOIN app_department d\n" + "ON a.DEPARTMENT_ID=d.ID\n" + "WHERE a.MOBILE =?", phone);
				users1.forEach(record -> {
					Map<String, Object> peopleinfo = new HashMap<>();
					record.toMap().forEach((k, v) -> {
						if ("locationName".equalsIgnoreCase(k)) {
							peopleinfo.put("locationName", v);
						} else
							peopleinfo.put(k.toLowerCase(), v);
					});

					peopleinfo.put("locationName", LocationEnum.getName(peopleinfo.get("location") + ""));
					peopleinfo.put("userid", peopleInfo.getUserId() + "");
					listObject.add(peopleinfo);
				});
				Map<String, Object> p = new HashMap<>();
				p.put("phone", phone);
				p.put("account", listObject);
				list.add(p);

				// todo记录日志
				// Log.getLog("PeopleService").info("skfjaslllllll");
			}
		} catch (Exception e) {
			// todo记录日志
			System.out.println(e.getMessage());
		}
		return list;
	}

	// 通过钉钉用户ID获取系统用户信息
	// 1、先调用钉钉接口获取手机号
	// 2、通过手机号在系统里进行查询
	public List<Map<String, Object>> getPeopleInfo(PeopleInfo peopleInfo) {
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			for (String userID : peopleInfo.getUserIds()) {
				String detailStr = userDingTalkService.getUserDetail(userID);
				Map<String, Object> map2 = JsonUtil.toRes(detailStr);
				String phone = String.valueOf(map2.get("mobile"));

				List<Map<String, Object>> listObject = new ArrayList<>();
				List<Record> users1 = Db.find("SELECT \n" + "a.USERNAME AS username\n" + ",a.NAME AS name\n"
						+ ",a.MOBILE AS mobile\n" + ",a.EMAIL AS email\n" + ",a.LOCATIONID AS location\n"
						+ ",a.DEPARTMENT_ID AS department\n" + ",d.NAME AS deparmentname\n" + ",c.ID AS stationid\n"
						+ ",c.NAME AS station\n" + "from app_user a\n" + "inner join app_group_users  b\n"
						+ "on a.id=b.USER_ID\n" + "inner join app_group c\n" + "on b.GROUP_ID=c.id\n"
						+ "inner JOIN app_department d\n" + "ON a.DEPARTMENT_ID=d.ID\n" + "WHERE a.MOBILE =?", phone);
				users1.stream().forEach(record -> {
					Map<String, Object> peopleinfo = record.toMap();
					peopleinfo.put("locationName", LocationEnum.getName(peopleinfo.get("location") + ""));
					peopleinfo.put("userid", peopleInfo.getUserId() + "");
					listObject.add(peopleinfo);
				});
				Map<String, Object> p = new HashMap<>();
				p.put("phone", phone);
				p.put("account", listObject);
				list.add(p);

				// todo记录日志
				// Log.getLog("PeopleService").info("skfjaslllllll");
			}
		} catch (Exception e) {
			// todo记录日志
			System.out.println(e.getMessage());
		}
		return list;
	}

	// for登入
	public List<Map<String, Object>> getPeopleInfoFor(PeopleInfo peopleInfo) {
		List<Map<String, Object>> list = new ArrayList<>();
		List<Record> users = Db.find("SELECT c.id as id,a.USERNAME AS username,a.NAME AS name,a.MOBILE AS mobile"
				+ ",a.EMAIL AS email,a.LOCATIONID AS location,a.DEPARTMENT_ID AS department\n"
				+ ",c.ID AS groupid,c.NAME AS groupname\n" + ",d.NAME AS deparmentname\n" + ", '"
				+ peopleInfo.getUserId() + "' AS userid \n " + "from app_user a\n" + "inner join app_group_users  b\n"
				+ "on a.id=b.USER_ID\n" + "inner join app_group c\n" + "on b.GROUP_ID=c.id\n"
				+ "inner JOIN app_department d\n" + "ON a.DEPARTMENT_ID=d.ID\n" + "WHERE a.MOBILE=?",
				peopleInfo.getMobile().get(0));

		users.stream().forEach(record -> {
			Map<String, Object> peoinfo = record.toMap();
			peoinfo.put("locationName", LocationEnum.getName(peoinfo.get("location") + ""));
			list.add(peoinfo);
		});
		return list;
	}

}
